﻿using System.Collections.Generic;
using System.Linq;
using Rated.DAL.Db;
using System.Web.Mvc;
using System.Data.Objects.SqlClient;

namespace Rated.Website.Helpers
{
    public static class LocationHelper
    {
        public static List<SelectListItem>Continents()
        {
            var db = new RatedEntities();
            var result = db.Continents.OrderBy(o => o.Name).Select(o=> new SelectListItem {Text=o.Name,Value= SqlFunctions.StringConvert((double)o.ID).Trim()}).ToList();
            result.Insert(0,new SelectListItem {Text = "Worldwide", Value = "0"});
            return result;
        }

        public static List<SelectListItem> Countries(int continentId)
        {
            var db = new RatedEntities();
            var result = db.Countries.Where(o => o.ContinentID == continentId).OrderBy(o => o.Name).Select(o => new SelectListItem { Text = o.Name, Value = SqlFunctions.StringConvert((double)o.ID).Trim() }).ToList();
            result.Insert(0, new SelectListItem { Text = "All Countries", Value = "0" });
            return result;
        }

        public static List<SelectListItem> Localities(int countryId)
        {
            var db = new RatedEntities();
            var result = db.Localities.Where(o => o.CountryID == countryId).OrderBy(o => o.Name).Select(o => new SelectListItem { Text = o.Name, Value = SqlFunctions.StringConvert((double)o.ID).Trim() }).ToList();
            result.Insert(0, new SelectListItem { Text = "All Locations", Value = "0" });
            return result;
        }

        public static List<SelectListItem> States(int localityId)
        {
            var db = new RatedEntities();
            var result = db.States.Where(o => o.LocalityID == localityId).OrderBy(o => o.Name).Select(o => new SelectListItem { Text = o.Name, Value = SqlFunctions.StringConvert((double)o.ID).Trim() }).ToList();
            result.Insert(0, new SelectListItem { Text = "All States", Value = "0" });
            return result;
        }

        public static List<SelectListItem> Cities(int stateId)
        {
            var db = new RatedEntities();
            var result = db.Cities.Where(o => o.StateID == stateId).OrderBy(o => o.Name).Select(o => new SelectListItem { Text = o.Name, Value = SqlFunctions.StringConvert((double)o.ID).Trim() }).ToList();
            result.Insert(0, new SelectListItem { Text = "All Cities", Value = "0" });
            return result;
        }

    }
}